package com.bw.permission.utils;

import org.apache.pdfbox.io.IOUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DownLoadUtil {
	/**
	 * 下载预览文件
	 * @param response
	 * @param source 源文件
	 * @param downloadName 下载的文件名
	 * @param mode 模式 0 下载 1 预览
	 * @throws IOException
	 */
	public static void download(HttpServletResponse response,String source,String downloadName,int mode)throws IOException{
		String modeStr = "inline";
		if(mode == 0){
			modeStr = "attachment";
			downloadName = new String(downloadName.getBytes("utf-8"), "iso-8859-1");
		}
		
		response.setHeader("Content-Disposition", modeStr+";fileName=" + downloadName);
		
		InputStream iStream = new FileInputStream(source);
		IOUtils.copy(iStream, response.getOutputStream());
		iStream.close();
	}
}
